package com.smfx.yxt.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.smfx.yxt.common.RspResult;
import com.smfx.yxt.common.enums.Status;
import com.smfx.yxt.daoSqlserver.FinalTermScoreReportMapper;
import com.smfx.yxt.daoSqlserver.StudentInfoMapper;
import com.smfx.yxt.model.FinalTermScoreReport;
import com.smfx.yxt.model.StudentInfo;
import com.smfx.yxt.reqVo.BaseQueryVO;
import com.smfx.yxt.reqVo.StudentReqVO;
import com.smfx.yxt.service.IFinalTermScoreReportService;
import com.smfx.yxt.utils.StringUtil;

@Service("finalTermScoreReportService")
public class FinalTermScoreReportServiceImpl implements IFinalTermScoreReportService {

	@Autowired
	private FinalTermScoreReportMapper finalTermScoreReportMapper;
	@Autowired
	private StudentInfoMapper studentInfoMapper;
	
	@Override
	public int insertSelective(FinalTermScoreReport record) {
		return finalTermScoreReportMapper.insertSelective(record);
	}

	@Override
	public FinalTermScoreReport selectByPrimaryKey(Integer id) {
		return finalTermScoreReportMapper.selectByPrimaryKey(id);
	}

	@Override
	public int updateByPrimaryKeySelective(FinalTermScoreReport record) {
		return finalTermScoreReportMapper.updateByPrimaryKeySelective(record);
	}

	@Override
	public List<FinalTermScoreReport> selectClassTermReport(BaseQueryVO vo) {
		return finalTermScoreReportMapper.selectClassTermReport(vo);
	}

	@Override
	public FinalTermScoreReport selectStudentTermReport(BaseQueryVO vo) {
		return finalTermScoreReportMapper.selectStudentTermReport(vo);
	}

	@Override
	public int delClassTermReport(BaseQueryVO vo) {
		return finalTermScoreReportMapper.delClassTermReport(vo);
	}

	@Override
	public RspResult addImportTermScoreReport(List<FinalTermScoreReport> list, BaseQueryVO vo) {
		StudentReqVO studentReqVO = new StudentReqVO();
		studentReqVO.setGrade(Integer.valueOf(vo.getGrade()));
		studentReqVO.setClazz(vo.getClazz());
		List<StudentInfo> studentList = studentInfoMapper.selectListByParam(studentReqVO);
		List<String> errorList = new ArrayList<>();
		Map<String, Integer> map = getClassStudentMap(studentList);
		List<FinalTermScoreReport> filterList = new ArrayList<>();
		for(FinalTermScoreReport report : list) {
			if(StringUtil.isNotNullOrZero(report.getStudentNo())) {
				String key = report.getStudentNo() + report.getStudentName();
				if(!map.containsKey(key)) {
					errorList.add("学号" + report.getStudentNo() + "的学生姓名与班级学生匹配不上");
					continue;
				}
				report.setStudentId(map.get(key));
				report.setTermId(vo.getTermId());
				report.setTermType(vo.getTermType());
				filterList.add(report);
			}
		}
		if(!StringUtil.isNullOrEmpty(errorList)) {
			return new RspResult(Status.FAIL.getCode(), String.join(",", errorList));
		}
		finalTermScoreReportMapper.delClassTermReport(vo);
		List<FinalTermScoreReport> insertList = new ArrayList<>();
		for(int i = 0; i < filterList.size(); i++) {
			insertList.add(filterList.get(i));
			if((i + 1) % 30 == 0) {
				finalTermScoreReportMapper.insertTermScoreReport(insertList);
				insertList = new ArrayList<>();
			}
		}
		if(!StringUtil.isNullOrEmpty(insertList)) {
			finalTermScoreReportMapper.insertTermScoreReport(insertList);
		}
		return new RspResult(Status.SUCCESS);
	}
	public static void main(String[] args) {
		System.out.println(0%20);
	}

	/**
     * 查询班级学生map
     * key: 姓名+学号 value: ID
     * @param grade, class
     * @return
     */
    private static Map<String, Integer> getClassStudentMap(List<StudentInfo> list) {
    	Map<String, Integer> map = new HashMap<>();
    	for(StudentInfo student : list) {
    		String key = student.getStudentId() + student.getName();
    		if(!map.containsKey(key)) {
    			map.put(key, student.getId());
    		}
    	}
    	return map;
    }
}
